package net.vkits.controller;

import net.vkits.anjiamiao.application.PayRecordApplicationService;
import net.vkits.anjiamiao.domain.model.PayRecord;
import net.vkits.common.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 2016/12/15.
 */
@Controller
@RequestMapping("/payrecord")
public class PayRecordController {
    private static final Logger logger = LoggerFactory.getLogger(PayRecordController.class);

    @Resource
    private PayRecordApplicationService payRecordApplicationService;

    @RequiresPermissions("payrecord:view")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView payrecord() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "payrecord");
        modelMap.put("sidebar", "payrecord");
        modelMap.put("currentUser",username);
        return new ModelAndView("/payrecord", modelMap);
    }

    @RequiresPermissions("payrecord:view")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<PayRecord> getPayRecord(@PageableDefault(value = 10, sort = {"gmtCreate"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<PayRecord> payRecords = payRecordApplicationService.getPayRecord(pageable);
        logger.info("size is: {}", payRecords.getTotalElements());
        return payRecords;
    }

}
